<template>
  <view class="login-container">
    <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
    <button type="primary" class="btn-login"  @click="getuserprofile">一键登录</button>
    <text class="text-login">登录后尽享更多权益</text>
  </view>
</template>

<script>
  import { $http } from '@escook/request-miniprogram';
  import {mapMutations,mapState} from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        code: ''
        
      };
    },
   computed:{
     ...mapState('m_user',['redirectInfo'])
   },
    methods:{
      ...mapMutations('m_user',['undateUserInfo','updataToken','updateRedirectInfo']),
      // 获取用户信息
    getuserprofile(e){
        uni.getUserProfile({
          desc: 'wexin'
        }).then(res=>{
          // 进行调用进行获取到code值
          const token=this.getToken()
          
          token.then((result)=>{
            console.log(result.code)
            //进行存储
            this.undateUserInfo(res[1].userInfo)
            // console.log(res[1].userInfo);
            //准备参数
            const query={
              code: result.code,
              encryptedData: res[1].encryptedData,
              iv: res[1].iv,
              rawData: res[1].rawData,
              signature: res[1].signature
            }
           
           // 进行登录
           this.postLogin(query)
           
          })
        })
      },
      //获取到code的值
     async getToken(){
        const [err,res] = await uni.login().catch(err=>err)
        if(err ||res.errMsg !=="login:ok") return uni.$showMsg('登录失败！')
        return res
      },
      
      //发起登录请求
     async postLogin(query){
       console.log(query);
        const {data:loginResult} =await $http.post('/api/public/v1/users/wxlogin',query)
        // if(loginResult.meta.status!==200) return uni.$showMsg("登录失败！")
        uni.$showMsg("登录成功！")
        //把token进行保存到token中
       // 进行自定义一个loginResult.messsage.token的值,后续要从服务器获取到 TDTO
        const token="Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
        this.updataToken(token)
        
        // 登录成功后进行跳转
        this.navigateBack()
      },
      navigateBack(){
        if(this.redirectInfo && this.redirectInfo.openType === 'switchTab')
        uni.switchTab({
          url: this.redirectInfo.from,
          complete: ()=>{
            // 指定完成后，清除重定向
            this.updateRedirectInfo(null)
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .login-container{
    height: 750rpx;
    background-color: #F8f8f8;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
    &::after{
      content: '';
      display: block;
      width: 100%;
      height: 40px;
      background-color: white;
      position: absolute;
      bottom: 0;
      left: 0;
      border-radius: 100%;
      transform: translateY(50%);
    }
    .btn-login{
      width: 90%;
      margin: 15px 0;
      border-radius: 100px;
      background-color: #c00;
    }
    .text-login{
      font-size: 12px;
      
    }
  }
</style>